package com.kailong.service.Impl.sys;

import cn.hutool.core.util.BooleanUtil;
import com.kailong.dto.sys.DeptTreeDTO;
import com.kailong.entity.ResponsePageEntity;
import com.kailong.entity.sys.DeptConditionEntity;
import com.kailong.entity.sys.DeptEntity;
import com.kailong.mapper.CustomerMapper;
import com.kailong.mapper.sys.DeptConditionMapper;
import com.kailong.mapper.sys.DeptMapper;
import com.kailong.service.BaseService;
import com.kailong.service.sys.DeptService;
import com.kailong.util.AssertUtil;
import com.kailong.util.BetweenTimeUtil;
import com.kailong.util.ExcelUtil;
import com.kailong.util.FillUserUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * created by kailong on 2025/9/3
 */
@Service
public class DeptServiceImpl extends BaseService<DeptEntity, DeptConditionEntity> implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private DeptConditionMapper deptConditionMapper;

    @Override
    protected CustomerMapper getBaseMapper() {
        return deptMapper;
    }

    @Override
    public ResponsePageEntity<DeptTreeDTO> searchByPage(DeptConditionEntity deptConditionEntity) {
        int count = deptMapper.searchCount(deptConditionEntity);
        if (count == 0) {
            return ResponsePageEntity.buildEmpty(deptConditionEntity);
        }
        List<DeptEntity> dataList = deptMapper.searchByCondition(deptConditionEntity);
        List<DeptTreeDTO> deptTreeEntities = buildDeptTree(dataList, deptConditionEntity.getQueryTree());
        return ResponsePageEntity.build(deptConditionEntity, count, deptTreeEntities);
    }

    private List<DeptTreeDTO> buildDeptTree(List<DeptEntity> dataList, Boolean queryTree) {
        if (CollectionUtils.isEmpty(dataList)) {
            return Collections.emptyList();
        }
        List<DeptTreeDTO> deptTreeDTOList = dataList.stream().map(x -> convertToDeptTreeDTO(x)).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(deptTreeDTOList)) {
            return Collections.emptyList();
        }

        if (BooleanUtil.isTrue(queryTree)) {
            for (DeptTreeDTO deptTreeDTO : deptTreeDTOList) {
                buildChildren(deptTreeDTO);
            }
        }

        return deptTreeDTOList;
    }
    private DeptTreeDTO convertToDeptTreeDTO(DeptEntity deptEntity) {
        DeptTreeDTO deptTreeDTO = new DeptTreeDTO();
        deptTreeDTO.setId(deptEntity.getId());
        deptTreeDTO.setName(deptEntity.getName());
        deptTreeDTO.setLabel(deptEntity.getName());
        deptTreeDTO.setPid(deptEntity.getPid());
        deptTreeDTO.setCreateTime(deptEntity.getCreateTime());
        return deptTreeDTO;
    }

    private void buildChildren(DeptTreeDTO deptTreeDTO) {
        DeptConditionEntity deptConditionEntity = new DeptConditionEntity();
        deptConditionEntity.setPid(deptTreeDTO.getId());
        deptConditionEntity.setPageSize(0);
        List<DeptEntity> deptEntities = deptMapper.searchByCondition(deptConditionEntity);
        if (CollectionUtils.isNotEmpty(deptEntities)) {
            for (DeptEntity deptEntity : deptEntities) {
                DeptTreeDTO childDeptTreeDTO = convertToDeptTreeDTO(deptEntity);
                deptTreeDTO.addChildren(childDeptTreeDTO);
                buildChildren(childDeptTreeDTO);
            }
        }
    }
    /**
     * 新增部门
     *
     * @param deptEntity
     * @return
     */
    @Override
    public int insert(DeptEntity deptEntity) {
        if (Objects.isNull(deptEntity.getPid())) {
            deptEntity.setPid(0L);
        }
        FillUserUtil.fillCreateUserInfo(deptEntity);
        Date date = new Date();
        deptEntity.setCreateTime(date);
        return deptMapper.insert(deptEntity);
    }
    /**
     * 修改部门
     *
     * @param deptEntity
     * @return
     */

    @Override
    public int update(DeptEntity deptEntity) {
        AssertUtil.notNull(deptEntity.getId(), "ID不能为空");
        FillUserUtil.fillUpdateUserInfo(deptEntity);
        deptEntity.setUpdateTime(new Date());
        return deptMapper.updateById(deptEntity);
    }
    /**
     * 批量删除部门
     *
     * @param ids
     * @return
     */

    @Override
    public int deleteByIds(List<Long> ids) {
        List<DeptEntity> deptEntities = deptMapper.selectBatchIds(ids);
        AssertUtil.notEmpty(deptEntities, "部门已被删除");

        DeptEntity deptEntity = new DeptEntity();
        FillUserUtil.fillUpdateUserInfo(deptEntity);
        return deptMapper.deleteBatchIds(ids);
    }
    /**
     * 导出部门数据
     *
     * @param response
     * @param deptConditionEntity
     * @throws IOException
     */

//    @Override
//    public void export(HttpServletResponse response, DeptConditionEntity deptConditionEntity) throws  IOException{
//        BetweenTimeUtil.parseTime(deptConditionEntity);
//        deptConditionEntity.setPageSize(0);
//        List<DeptEntity> deptEntities = deptMapper.searchByCondition(deptConditionEntity);
//        ExcelUtil.exportExcel("部门数据", DeptEntity.class, deptEntities, response);
//    }
}
